using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections.Generic;
using XProject.DAO;
namespace XProject.GUI
{
    public partial class AccountManager : Form
    {
        AccountDAO daoAccount = new AccountDAO();
        public AccountManager()
        {
            InitializeComponent();
        }

        public void AddListItem(Entity.Account acc)
        {
            ListViewItem item = new ListViewItem(acc.Username);
            if (acc.Role == 1)
                item.SubItems.Add("Admin");
            else item.SubItems.Add("User");
            item.Tag = acc;
            lsvAccount.Items.Add(item);
        }
        private void AccountManager_Load(object sender, EventArgs e)
        {
            cmbRole.Items.Add(new Util.ListItem("Administrator",1));
            cmbRole.Items.Add(new Util.ListItem("User",2));
            cmbRole.SelectedIndex = 1;
            IList<Entity.Account> accList = new List<Entity.Account>();
            accList = daoAccount.GetAllAccount();
            if (accList.Count > 0)
            {
                foreach (Entity.Account acc in accList)
                {
                    AddListItem(acc);
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            
        }

        private void lsvAccount_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lsvAccount.SelectedItems.Count <= 0) return;
            Entity.Account acc = lsvAccount.SelectedItems[0].Tag as Entity.Account;
            txtUsername.Text = acc.Username;
            cmbRole.SelectedIndex = acc.Role - 1;
          
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (lsvAccount.SelectedItems.Count <= 0)
            {
                MessageBox.Show("Please select an item");
                return;
            }
            //warning
            if (MessageBox.Show("WARNING: Remove an account will clear all relative data of this account (diary, events, ...)\nAre you sure want to continue?","Warning",MessageBoxButtons.YesNo,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button2) != DialogResult.Yes) return;            
            //confirm password
            GUI.Login frmLogin = new Login();
            frmLogin.Text = "Enter password";
            frmLogin.UsernameBox.Text = MainForm.CurrentAccount.Username;
            frmLogin.UsernameBox.Enabled = false;
            if (frmLogin.ShowDialog() != DialogResult.Yes) return;
            Entity.Account acc = lsvAccount.SelectedItems[0].Tag as Entity.Account;
            if (acc.AccountID == MainForm.CurrentAccount.AccountID)
            {
                MessageBox.Show("Cannot delete current account. Action is aborted");
                return;
            }
            if (daoAccount.Delete(acc) > 0)
            {
                MessageBox.Show("The selected account has been deleted succefully.");
                lsvAccount.Items.Remove(lsvAccount.SelectedItems[0]);
            }
            else MessageBox.Show(daoAccount.Message,"Error");
        }
    }
}